SeleniumNote
=Selenium Webdriver= * Selenium Webdriver API download ** WebElement ** forms * Selenium Webdriver replaces WebDriverBackedSelenium(Legacy Selenium in its previous relase). * http://www.seleniumhq.org/download/ * https://sites.google.com/a/chromium.org/chromedriver/downloads Chrome Driver WebDriver driver = new ChromeDriver(); Firefox Driver // initialize and start the browser driver = new FirefoxDriver(); // implicit wait for 10s // driver.manage().timeouts().implicitlyWait(1s, TimeUnit.SECONDS); // find the first element:h5 // NO need assertion, as findElement() throws: NoSuchElementException - If no matching elements are found WebElement h5 = driver.findElement(By.tagName("h5")); // close browser driver.close(); Android Driver import junit.framework.TestCase; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.android.AndroidDriver; public class OneTest extends TestCase { public void testGoogle() throws Exception { WebDriver driver = new AndroidDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } } JavascriptExecutor xvfb * http://stackoverflow.com/questions/6183276/how-do-i-run-selenium-in-xvfb =Locate Elements= findElement/s List links = driver.findElements(By.tagName("a")); WebElement gmailLink = driver.findElement(By.linkText("GMail")); WebElement inboxLink = driver.findElement(By.partialLinkText("Inbox")); WebElement table = driver.findElement(By.id("summaryTable")); CSS Selector // absolute path WebElement userName = driver.findElement(By.cssSelector("html body div div form input")); // or with '>' WebElement userName = driver.findElement(By.cssSelector("html > body > div > div > form > input")); // relative path WebElement userName = driver.findElement(By.cssSelector("input")); // by class WebElement loginButton = driver.findElement(By.cssSelector("input.login")); WebElement loginButton = driver.findElement(By.cssSelector(".login")); // by id selector WebElement userName = driver.findElement(By.cssSelector("input#username")); WebElement userName = driver.findElement(By.cssSelector("#username")); // by attribute selector WebElement userName = driver.findElement(By.cssSelector("inputname=username")); WebElement previousButton = driver.findElement(By.cssSelector("imgalt='Previous'")); // multiple attributes WebElement previousButton = driver.findElement(By.cssSelector("inputtype='submit'value='Login'")); // by attribute name List imagesWithAlt = driver.findElements(By.cssSelector("imgalt")); // not function List imagesWithoutAlt = driver.findElements(By.cssSelector("img:not(alt)")); * attribute value starts with: inputid^='ctrl' * attribute value ends with: inputid$='_userName' * attribute value contains: inputid*='userName' CSS Function // contains() WebElement cell = driver.findElement(By.cssSelector("td:contains('Item 1')")); //innerText attribute WebElement cell = driver.findElement(By.cssSelector("td1'")); // firefox version of innerText WebElement cell = driver.findElement(By.cssSelector("td1'")); CSS Pseudo Class //tr:nth-child(even) tr:nth-child(odd) or tr:nth-child(2) // :first-child :last-child WebElement userName = driver.findElement(By.cssSelector("form#loginForm :nth-child(2)")); WebElement productDescription = driver.findElement(By.cssSelector("input:focus")); Read more about pseudo class http://www.w3schools.com/cssref/css_selectors.asp. XPath Selector // by absolute path WebElement userName = driver.findElement(By.xpath("html/body/div/div/form/input")); //relative path WebElement userName = driver.findElement(By.xpath("//input")); // by index WebElement userName = driver.findElement(By.xpath("//input2")); // by attribute WebElement userName = driver.findElement(By.xpath("//input@id='username'")); // by multiple attributes WebElement previousButton = driver.findElement(By.xpath("//input@type='submit'@value='Login'")); // 'and' operator WebElement previousButton = driver.findElement(By.xpath("//input@value='Login'")); // 'or' operator WebElement previousButton = driver.findElement(By.xpath("//input@value='Login'")); // by attribute name List imagesWithAlt = driver.findElements(By.xpath ("img@alt")); // starts with ...inputstarts-with(@id,'ctrl')... // ends with ...inputends-with(@id,'_userName')... // contains ...inputcontains(@id,'userName')... // by attribute value, any attribute WebElement userName = driver.findElement(By.xpath("//input@*='username'")); XPath Axis * //td1'/'ancestor::'table * /table/'descendant::'td/input * //td1'/'following::'tr * //td1'/'following-sibling::'td * //tdtext()='$150'/'preceding::'tr * //tdtext()='$150'/'preceding-sibling::'td XPath Functions // contains() + text() WebElement cell = driver.findElement(By.xpath("//td1')")); //exact text WebElement cell = driver.findElement(By.xpath("//td1'")); Selenium with Flash * http://code.google.com/p/flex-ui-selenium/ * http://www.adobe.com/devnet/flash/articles/flash_selenium.html Moving Between Windows and Frames ; Pop Up driver.switchTo().window("HelpWindow"); ; Alert Alert alert = driver.switchTo().alert(); // sendKeys works for prompt alert.sendKeys("Foo"); alert.accept(); =Flow Control= Implicit Wait ; Synchronizing a test with an implicit wait * The default setting is 0 * Wait for a defined amount of time for the element to appear in the DOM ** In other terms, an implicit wait polls the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. * Minimize or avoid using an implicit wait in your tests and try to handle synchronization issues with an explicit wait. //Go to the Demo AJAX Application WebDriver driver = new FirefoxDriver(); driver.get("http://dl.dropbox.com/u/55228056/AjaxDemo.html"); //Set the Implicit Wait time Out to 10 Seconds driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Explicit Wait * WebDriverWait WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.titleContains("selenium")); =Send Events= * actions * action ** ButtonReleaseAction, ClickAction, ClickAndHoldAction, CompositeAction, ContextClickAction, DoubleClickAction, DoubleTapAction, DownAction, FlickAction, KeyDownAction, KeyUpAction, LongPressAction, MoveAction, MoveMouseAction, MoveToOffsetAction, PauseAction, ScrollAction, SendKeysAction, SingleTapAction, UpAction Double Click Actions action = new Actions(driver); action.doubleClick(myElemment); action.perform(); Multiple Select WebDriver driver = new FirefoxDriver(); driver.get("http://book.theautomatedtester.co.uk/multi-select.html"); Actions builder = new Actions(driver); WebElement select = driver.findElement(By.tagName("select")); List options = select.findElements(By.tagName("options")); Action multipleSelect = builder.keyDown(Keys.SHIFT) .click(options.get(0)) .click(options.get(2)) .build(); multipleSelect.perform(); =Form Control= ; Attach file WebElement inputFilePath = driver.findElement(By.id("filepath")); inputFilePath.sendKeys("/absolute/path/to/my/local/file"); WebElement upload = driver.findElement(By.id("myfile")); upload.sendKeys("/abs/path/to/file/lightbulb.jpg"); driver.findElement(By.id("submit")).click(); =Screenshot= WebElement ele = driver.findElement(By.id("hplogo")); // Get entire page screenshot File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); BufferedImage fullImg = ImageIO.read(screenshot); // Get the location of element on the page Point point = ele.getLocation(); // Get width and height of the element int eleWidth = ele.getSize().getWidth(); int eleHeight = ele.getSize().getHeight(); // Crop the entire page screenshot to get only element screenshot BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); ImageIO.write(eleScreenshot, "png", screenshot); // Copy the element screenshot to disk FileUtils.copyFile(screenshot, new File("screenshot.png")); =PageObject= PageFactory ; factory code ... public AfterLoginPage clickLogin(){ // click login button return PageFactory.initElements(driver, AfterLoginPage.class); } ... } ; AfterLoginPage public class AfterLoginPage{ ... public AfterLoginPage(WebDriver driver){ this.driver = driver; // ... load items for the page } } =Selenium Grid= Hub java -jar selenium-server-standalone-n.nn.nn.jar -role hub Node java -jar selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register #windows only node java -jar selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register \ -browser browserName="internet explorer",maxInstances=1,platform=WINDOWS Test @Before public void setUp() { // capability DesiredCapabilities capability = DesiredCapabilities.firefox(); capability.setBrowserName("firefox"); // Set the platform we want our tests to run on capability.setPlatform("LINUX"); // new driver driver = new RemoteWebDriver(new URL("http://:4444/wd/hub"), capability); // driver.get driver.get("http://book.theautomatedtester.co.uk/chapter1"); } @After public void tearDown() { driver.quit(); } @Test public void testExamples() { // We will put examples in here } Parallel TestNG plan1.xml lanunch java org.testng.TestNG plan1.xml java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs testng.xml java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs test-outputs\testng-failed.xml parallel ; parallel suites java org.testng.TestNG -suitethreadpoolsize 3 plan1.xml plan2.xml plan3.xml ; parallel methods/cases Continue reading at parallel-tests. =Usages= Attachement Popup =Books= * Packt.Selenium.2.Testing.Tools.2nd.Oct.2012 * Packt.Selenium.Testing.Tools.Cookbook.2012